// 在这里面书写如何处理每一个文件类型
// 引入gulp
var gulp = require("gulp");
var { src, dest, watch, series, parallel } = gulp;
var sass = require("gulp-sass");
var autoPreFixer = require("gulp-autoprefixer");
var cssmin = require("gulp-cssmin");
var babel = require("gulp-babel");
var uglify = require("gulp-uglify");
var htmlmin = require("gulp-htmlmin");
var imagemin = require("gulp-imagemin");
var clean = require("gulp-clean");
var server = require("gulp-webserver");
 


// 新的写法
function doJS() {
    return gulp.src("./src/js/*")
        .pipe(babel({
            presets: ["es2015"]
        }))
        .pipe(uglify())
        .pipe(gulp.dest("./dist/js/"))
}

function doScss() {
    return gulp.src("./src/css/*.scss")
        .pipe(sass())
        .pipe(autoPreFixer())
        .pipe(cssmin())
        .pipe(gulp.dest("./dist/css"))
}

function doHTML() {
    return gulp.src("./src/**/*.html")
        .pipe(htmlmin({
            "collapseWhitespace": true,
            "minifyCSS": true,
            "minifyJS": true,
            "removeEmptyAttributes": true,
            "ignoreCustomComment": true,
            "removeComments": true
        }))
        .pipe(gulp.dest("./dist/"))
}

function doImages() {
    return src("./src/images/*.*")
    // .pipe(imagemin())
        .pipe(dest("./dist/images"))

}

function doLibs() {
    return src("./src/libs/**/*.*")
        .pipe(dest("./dist/libs/"))
}

function remove() {
    return gulp.src("./dist/")
    .pipe(clean())
}

function listen() {
    return watch("./src/", series(remove, parallel(doJS, doScss, doHTML, doImages, doLibs)))
}

function webserver () {
    return src("./dist/")
    .pipe(server({
         host: "localhost",
         port: 3000,
         livereload: true,
         open: "./html/list.html",
         proxies: [
             {
                 source:"/php/",
                 target: "http://localhost:80/php/",
             }
         ]
    }))
}

// 新写法的 配套任务定义方式 
module.exports.doJS = doJS;
module.exports.doScss = doScss;
module.exports.doHTML = doHTML;
module.exports.doImages = doImages;
module.exports.doLibs = doLibs;
module.exports.remove = remove;

module.exports.doAll =  series(remove, parallel(doJS, doScss, doHTML, doImages, doLibs))

module.exports.listen = listen;

module.exports.webserver = webserver;